<template>
  <div class="vip_privilege">
    <el-container>
      <el-header>
        <header-item :item="tips"></header-item>
      </el-header>
      <el-main>
        <main-tools-item :toolsType="toolsType" @add='add'></main-tools-item>
        <main-spare-item @onClick=''></main-spare-item>
        <main-data-list-item :columns="columns" :dataList="dataList" :pager="page" @batchDelete='batchDelete' @currentChange='currentChange'>
          <el-table-column label="操作" slot="table_oper" min-width="160px">
            <template slot-scope="scope">
              <el-button size="mini" type="success" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </main-data-list-item>
      </el-main>
    </el-container>
    <!--新增弹框 -->
    <el-dialog title="新增" :visible.sync="addDialogVisible" width="30%" :before-close="addBeforeClose" close-on-press-escape close-on-click-modal
      class="defaultDialog" :modal="false">
      <el-form ref="addParam" :rules="addAndEditRules" :model="addParam" label-width="30%">
        <template v-for="(item, index) in addAndEditColumns">
          <el-form-item :label="item.label" :key="index" :prop="item.value">
            <el-select v-if="item.value =='status'" v-model="addParam[item.value]" placeholder="请选择">
              <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-select v-else-if="item.value =='vip'" v-model="addParam[item.value]" placeholder="请选择">
              <el-option v-for="item in vipOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-upload v-else-if="item.value =='icon'" class="upload-demo" :action="uploadUrl" :on-remove="addIconHandleRemove" :on-success="addIconHandleSuccess"
              :file-list="addIconFileList" list-type="picture" name="file" :limit="1" :headers="uploadHeaders">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
            <el-input v-else v-model="addParam[item.value]"></el-input>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button size="mini" type="primary" @click="addSumbit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!--编辑弹框 -->
    <el-dialog title="编辑" :visible.sync="editDialogVisible" width="30%" :before-close="editBeforeClose" close-on-press-escape
      close-on-click-modal class="defaultDialog" :modal="false">
      <el-form ref="editParam" :rules="addAndEditRules" :model="editParam" label-width="30%">
        <template v-for="(item, index) in addAndEditColumns">
          <el-form-item :label="item.label" :key="index" :prop="item.value">
            <el-select v-if="item.value =='status'" v-model="editParam[item.value]" placeholder="请选择">
              <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-select v-else-if="item.value =='vip'" v-model="editParam[item.value]" placeholder="请选择">
              <el-option v-for="item in vipOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-upload v-else-if="item.value =='icon'" class="upload-demo" :action="uploadUrl" :on-remove="editIconHandleRemove" :on-success="editIconHandleSuccess"
              :file-list="editIconFileList" list-type="picture" name="file" :limit="1" :headers="uploadHeaders">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
            <el-input v-else v-model="editParam[item.value]"></el-input>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button size="mini" type="primary" @click="editSumbit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import {
    get,
    post,
    pat,
    del,
    common,
    getRefresh
  } from "../../../../api/api";
  import headerItem from "./../../Commons/headerItem.vue";
  import mainToolsItem from "./../../Commons/mainToolsItem.vue";
  import mainSpareItem from "./../../Commons/mainSpareItem.vue";
  import mainDataListItem from "./../../Commons/mainDataListItem.vue";
  import config from "../../../../../static/config.js";
  export default {
    components: {
      headerItem,
      mainToolsItem,
      mainSpareItem,
      mainDataListItem,
      UE: () =>
        import ('../ue.vue')
    },
    data() {
      return {
        tips: "会员特权",
        uploadUrl: window.dict.url + "/goods/upFilePic",
        toolsType: 3,
        searchType: 5,
        sels: [],
        page: {
          pageNo: 1,
          limit: 10,
          sizes: [10, 50, 100],
          total: 0
        },
        columns: [{
            prop: 'id',
            label: '特权',
            width: '120px',
            align: 'center'
          },
          {
            prop: 'vipPrivilegeVip',
            label: '会员等级',
            width: '120px',
            align: 'center'
          },
          {
            prop: 'icon',
            label: '特权图标',
            width: '120px',
            align: 'center',
          },
          {
            prop: 'name',
            label: '特权名称',
            width: '',
            align: 'center'
          }, {
            prop: 'status',
            label: '状态',
            width: '120px',
            align: 'center'
          }, {
            prop: 'orderNum',
            label: '排序',
            width: '120px',
            align: 'center'
          }, {
            prop: 'createUser',
            label: '创建者',
            width: '120px',
            align: 'center'
          }, {
            prop: 'createTime',
            label: '创建时间',
            width: '',
            align: 'center'
          }
        ],
        dataList: [],
        loading: false,
        param: {
          name: '',
          size: '10',
          current: '1'
        },
        addParam: {
          status: 0
        },
        addAndEditColumns: [
          // {
          //   value: 'id',
          //   label: '特权id',
          // },
          {
            value: 'vip',
            label: '会员等级',
          },
          {
            value: 'icon',
            label: '特权图标',
          },
          {
            value: 'name',
            label: '特权名称',
          }, {
            value: 'status',
            label: '状态',
          }, {
            value: 'orderNum',
            label: '排序',
          }
        ],
        editParam: {},
        statusOptions: [{
          label: "不可用",
          value: 0
        }, {
          label: "可用",
          value: 1
        }],
        vipOptions: [],
        addAndEditRules: {
          roleName: [{
              required: true,
              message: '请输入内容',
              trigger: 'blur'
            },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          desc: [{
            required: true,
            message: '请输入内容',
            trigger: 'blur'
          }]
        },
        addDialogVisible: false,
        editDialogVisible: false,
        addIconFileList: [],
        editIconFileList: [],
        uploadHeaders: {
          'token': localStorage.getItem('token')
        },
      };
    },
    methods: {
      //--------新增----------
      addBeforeClose(done) {
        this.addIconFileList = []
        done()
      },
      add() {
        this.addParam = {
          'status': 1
        }
        this.addDialogVisible = true;
        this.getVipPriceList();
      },
      addSumbit() {
        this.addDialogVisible = false;
        this.addIconFileList = []
        this.addVipPrivilege();
      },
      addIconHandleRemove() {
        this.addParam.icon = ""
      },
      addIconHandleSuccess(response, file, fileList) {
        this.addParam.icon = response.data
      },
      //--------编辑----------
      editBeforeClose(done) {
        this.editIconFileList = []
        done()
      },
      handleEdit(index, row) {
        this.editDialogVisible = true;
        this.getVipPriceList();
        this.editParam = JSON.parse(JSON.stringify(row));
        if (row.icon && row.icon != " ") {
          this.editIconFileList = [{
            name: row.name,
            url: row.icon
          }]
        }
      },
      editSumbit() {
        this.editDialogVisible = false;
        this.editVipPrivilege();
      },
      editIconHandleRemove() {
        this.editParam.icon = " "
      },
      editIconHandleSuccess(response, file, fileList) {
        this.editParam.icon = response.data
      },
      //--------删除----------
      handleDelete(index, row) {
        this.delConfirm(row.id);
      },
      batchDelete(val) {},
      //-------分页----------
      currentChange(val) {
        this.param.current = "";
        this.param.current = val
        this.getVipPrivilegeList();
      },
      //获取list列表
      getVipPrivilegeList() {
        return new Promise((resolve, reject) => {
          var param = {};
          for (let key in this.param) {
            if (this.param[key] != null && this.param[key].length != 0) {
              param[key] = this.param[key]
            }
          }
          post("/userInfo/selectXasVipPrivilege", param).then(resdata => {
            let self = function () {
              this.$router.push({
                path: '/home'
              });
            }.bind(this);
            let error = function () {
              alert(resdata.msg)
            }.bind(this);
            let success = function () {
              if (resdata.code == 1) {
                this.dataList = resdata.data.records;
                this.page.total = resdata.data.total;
              }
            }.bind(this);
            common(self)(error)(success)(resdata);
            resolve();
          });
        });
      },
      //获取list列表
      getVipPriceList() {
        return new Promise((resolve, reject) => {
          post("/userInfo/selectAllXasVipPrice").then(resdata => {
            let self = function () {
              this.$router.push({
                path: '/home'
              });
            }.bind(this);
            let error = function () {
              alert(resdata.msg)
            }.bind(this);
            let success = function () {
              if (resdata.code == 1) {
                var list = resdata.data;
                var newList = [];
                const getStr = function (list, newList) {
                  for (var i = 0; i < list.length; i++) {
                    var midList = {};
                    midList.value = list[i].id;
                    midList.label = "svip" + list[i].id;
                    newList[i] = midList;
                  }
                }
                getStr(list, newList)
                this.vipOptions = newList;
              }
            }.bind(this);
            common(self)(error)(success)(resdata);
            resolve();
          });
        });
      },
      addVipPrivilege() {
        return new Promise((resolve, reject) => {
          var addParam = {};
          for (let key in this.addParam) {
            if (this.addParam[key] != null && this.addParam[key].length != 0) {
              addParam[key] = this.addParam[key]
            }
          }
          post("/userInfo/addXasVipPrivilege", addParam).then(resdata => {
            let self = function () {
              this.$router.push({
                path: '/home'
              });
            }.bind(this);
            let error = function () {
              alert(resdata.msg)
            }.bind(this);
            let success = function () {
              if (resdata.code == 1) {
                this.$message({
                  message: '新增成功！',
                  type: 'success',
                  duration: 2000,
                });
                this.getVipPrivilegeList();
              }
            }.bind(this);
            common(self)(error)(success)(resdata);
            resolve();
          });
        });
      },
      editVipPrivilege() {
        return new Promise((resolve, reject) => {
          var editParam = {};
          for (let key in this.editParam) {
            if (this.editParam[key] != null && this.editParam[key].length != 0) {
              editParam[key] = this.editParam[key]
            }
          }
          post("/userInfo/updateXasVipPrivilege", editParam).then(resdata => {
            let self = function () {
              this.$router.push({
                path: '/home'
              });
            }.bind(this);
            let error = function () {
              alert(resdata.msg)
            }.bind(this);
            let success = function () {
              if (resdata.code == 1) {
                this.$message({
                  message: '编辑成功！',
                  type: 'success',
                  duration: 2000,
                });
                this.getVipPrivilegeList();
              }
            }.bind(this);
            common(self)(error)(success)(resdata);
            resolve();
          });
        });
      },
      delConfirm(id) {
        this.$confirm('确定删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.delVipPrivilege(id);
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      delVipPrivilege(id) {
        return new Promise((resolve, reject) => {
          post("/userInfo/delXasVipPrivilege", {
            'id': id
          }).then(resdata => {
            let self = function () {
              this.$router.push({
                path: '/home'
              });
            }.bind(this);
            let error = function () {
              alert(resdata.msg)
            }.bind(this);
            let success = function () {
              if (resdata.code == 1) {
                this.$message({
                  message: '删除成功！',
                  type: 'success',
                  duration: 2000,
                });
                this.getVipPrivilegeList();
              }
            }.bind(this);
            common(self)(error)(success)(resdata);
            resolve();
          });
        });
      }
    },
    async mounted() {
      await this.getVipPrivilegeList();
    },
    watch: {}
  };

</script>

<style lang="scss">
  .vip_privilege {
    background-color: white;
    font-size: 1.8rem;
    width: 100%;
    height: 100%;
    .el-container {
      height: 100%;
      .el-header,
      .el-footer {
        background-color: #F9FAFE;
        color: #333;
        text-align: center;
        line-height: 50px;
        height: 50px !important;
        .header-child {
          height: 50px;
          width: 100%;
          text-align: left;
          padding-left: 20px;
          background-color: #cbe9eb;
          border-bottom: 1px solid black;
          box-shadow: 0 5px 5px 0 rgba(0, 0, 0, 0.5);
        }
      }
      .el-main {
        box-shadow: 0 5px 5px 0px rgba(0, 0, 0, 0.5); // margin: 10px 20px;
        margin: 10px;
        background-color: #fff;
        color: #333;
        text-align: center;
        height: 100%;
      }
    }
    /* 弹框CSS*/
    .defaultDialog {
      z-index: 1200 !important;
      .el-dialog {
        box-shadow: 0px 5px 15px 0 rgba(0, 0, 0, 0.5);
        border-radius: 5px;
        width: 50% !important;
        .el-dialog__header {
          text-align: left;
          .el-dialog__title {
            font-family: "Roboto", sans-serif;
            color: #7d7d7d;
            font-size: 15px;
          }
        }
        .el-dialog__body {
          .el-form {
            padding-left: 40px;
            .el-form-item {
              width: 100%;
              .el-form-item__content {
                text-align: left;
                width: 250px;
                .el-cascader {
                  width: 100%;
                }
                .el-select {
                  width: 100%;
                }
              }
              .el-form-item__label {
                padding: 0 50px 0 0;
                min-width: 80px;
                color: #333;
                font-weight: 400;
                font-size: 13px;
              }
            }
          }
        }
      }
    }
  }

</style>
